package com.witype.timeline;

import android.os.SystemClock;
import android.support.test.filters.LargeTest;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import com.witype.timeline.line.DataUtils;
import com.witype.timeline.line.PlayLine;
import com.witype.timeline.line.TimeLine;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;

/**
 * Created by WiType on 2017/4/16.
 * Email witype716@gmail.com
 * Desc:
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class PlayerTest implements TimeLine.OnAction {

    private static final int MILLI = 1000;

    public static final String TAG = "PlayerTest";

    /**
     * 单一测试
     *     |---------------|
     * pass
     */
    @Test
    public void testSingleScence()  {
        final Sence sence = getSenceFromNow("Sence 1",10,3);
        TimeLine timeLine = TimeLine.get();
        Log.e(TAG,"- - - - - - - - - - - - - - - - -");
        timeLine.start(this,new ArrayList<PlayLine>() {
            {
                add(sence);
            }
        });
        SystemClock.sleep(50000000);
    }

    /**
     * 未来时间点测试
     *    now ---> |---------------|
     * pass
     */
    @Test
    public void testFeature() {
        long now = System.currentTimeMillis();
        String begin = DataUtils.convertMilli2String(now + 7 * MILLI);
        String end = DataUtils.convertMilli2String(now + 25 * MILLI);
        final Sence sence2 = getSence("Sence 2",begin,end,4 * MILLI);
        TimeLine timeLine = TimeLine.get();
        Log.e(TAG,"- - - - - - - - - - - - - - - - -");
        timeLine.start(this,new ArrayList<PlayLine>() {
            {
                add(sence2);
            }
        });
        SystemClock.sleep(50000000);
    }

    /**
     * 未来时间点复杂测试
     *    now ---> |---------------|
     *                  |-------|
     *                                  |-------|
     * pass
     */
    @Test
    public void testFeatureComplex() {
        long now = System.currentTimeMillis();
        String begin = DataUtils.convertMilli2String(now + 7 * MILLI);
        String end = DataUtils.convertMilli2String(now + 25 * MILLI);
        final Sence sence = getSence("Sence 1",begin,end,4 * MILLI);

        String begin2 = DataUtils.convertMilli2String(now + 10 * MILLI);
        String end2 = DataUtils.convertMilli2String(now + 15 * MILLI);
        final Sence sence2 = getSence("Sence 2",begin2,end2,3 * MILLI);

        String begin3 = DataUtils.convertMilli2String(now + 30 * MILLI);
        String end3 = DataUtils.convertMilli2String(now + 45 * MILLI);
        final Sence sence3 = getSence("Sence 3",begin3,end3,2 * MILLI);

        TimeLine timeLine = TimeLine.get();
        Log.e(TAG,"- - - - - - - - - - - - - - - - -");
        timeLine.start(this,new ArrayList<PlayLine>() {
            {
                add(sence);
                add(sence2);
                add(sence3);
            }
        });
        SystemClock.sleep(50000000);
    }

    /**
     * 同时播放测试
     *    |---------|
     *    |---------|
     * pass
     */
    @Test
    public void testDouble(){
        final Sence sence = getSenceFromNow("Sence 1",5,2);
        final Sence sence2 = getSenceFromNow("Sence 2",5,2);
        TimeLine timeLine = TimeLine.get();
        Log.e(TAG,"- - - - - - - - - - - - - - - - -");
        timeLine.start(this,new ArrayList<PlayLine>() {
            {
                add(sence);
                add(sence2);
            }
        });
        SystemClock.sleep(50000000);
    }

    /**
     *  时间交叉测试
     *
     *     |---------------|
     *             |------------|
     */
    @Test
    public void testCross()  {
        final Sence sence = getSenceFromNow("Sence 1",15,2);
        long now = System.currentTimeMillis();
        String begin = DataUtils.convertMilli2String(now + 7 * MILLI);
        String end = DataUtils.convertMilli2String(now + 25 * MILLI);
        final Sence sence2 = getSence("Sence 2",begin,end,4 * MILLI);
        TimeLine timeLine = TimeLine.get();
        Log.e(TAG,"- - - - - - - - - - - - - - - - -");
        timeLine.start(this,new ArrayList<PlayLine>() {
            {
                add(sence);
                add(sence2);
            }
        });
        SystemClock.sleep(50000000);
    }

    /**
     * 未来时间点复杂测试
     *  |---------------|
     *             |---------------|
     *       |-------|
     *                        |--------|
     *                                      |--------|
     *                                      |--------|
     * pass
     */
    @Test
    public void testComplex() {
        long now = System.currentTimeMillis();
        String begin = DataUtils.convertMilli2String(now);
        String end = DataUtils.convertMilli2String(now + 25 * MILLI);
        final Sence sence = getSence("Sence 1",begin,end,4 * MILLI);

        String begin2 = DataUtils.convertMilli2String(now + 15 * MILLI);
        String end2 = DataUtils.convertMilli2String(now + 45 * MILLI);
        final Sence sence2 = getSence("Sence 2",begin2,end2,4 * MILLI);

        String begin3 = DataUtils.convertMilli2String(now + 10 * MILLI);
        String end3 = DataUtils.convertMilli2String(now + 20 * MILLI);
        final Sence sence3 = getSence("Sence 3",begin3,end3,3 * MILLI);

        String begin4 = DataUtils.convertMilli2String(now + 35 * MILLI);
        String end4 = DataUtils.convertMilli2String(now + 55 * MILLI);
        final Sence sence4 = getSence("Sence 4",begin4,end4,3 * MILLI);

        String begin5 = DataUtils.convertMilli2String(now + 60 * MILLI);
        String end5 = DataUtils.convertMilli2String(now + 70 * MILLI);
        final Sence sence5 = getSence("Sence 5",begin5,end5,3 * MILLI);

        final Sence sence6 = getSence("Sence 6",begin5,end5,3 * MILLI);

        TimeLine timeLine = TimeLine.get();
        Log.e(TAG,"- - - - - - - - - - - - - - - - -");
        timeLine.start(this,new ArrayList<PlayLine>() {
            {
                add(sence);
                add(sence2);
                add(sence3);
                add(sence4);
                add(sence5);
                add(sence6);
            }
        });
        SystemClock.sleep(50000000);
    }

    /**
     * test release
     */
    public void testRelease() {
        long now = System.currentTimeMillis();
        String begin = DataUtils.convertMilli2String(now);
        String end = DataUtils.convertMilli2String(now + 25 * MILLI);
        final Sence sence = getSence("Sence 1",begin,end,4 * MILLI);
        TimeLine timeLine = TimeLine.get();
        Log.e(TAG,"- - - - - - - - - - - - - - - - -");
        timeLine.start(this,new ArrayList<PlayLine>() {
            {
                add(sence);
            }
        });
        SystemClock.sleep(5000);
        timeLine.release();
        SystemClock.sleep(50000000);
    }

    @Override
    public void onAction(PlayLine playLine) {
        Sence sence = (Sence) playLine;
        Log.e(TAG,"playback play " + sence.getName());
    }

    @Override
    public void onEnd(PlayLine playLine) {
        Sence sence = (Sence) playLine;
        Log.e(TAG,"playback end " + sence.getName());
        Log.e(TAG,"+ + + + + + + + + + + + + + + + + +");
    }

    @Override
    public void onWait(long time) {
        Log.e(TAG,"play wait time = " + time);
    }

    @Override
    public void onNone() {
        Log.e(TAG,"play none");
    }

    private Sence getSence(String name,String beginTime, String endTime, long eachTime) {
        return new Sence(name,beginTime,endTime,eachTime);
    }

    private Sence getSenceFromNow(String name,int continueSec, long eachTime) {
        long now = System.currentTimeMillis();
        String begin = DataUtils.convertMilli2String(now);
        String end = DataUtils.convertMilli2String(now + continueSec * MILLI);
        return getSence(name,begin,end,eachTime * 1000);
    }
}